package org.jeecg.modules.zcglsupplies.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.activiti.entity.ActBusiness;
import org.jeecg.modules.activiti.service.IActBusinessService;
import org.jeecg.modules.zcgl.entity.AssetFixed;
import org.jeecg.modules.zcglsupplies.entity.ZcglSupplies;
import org.jeecg.modules.zcglsupplies.service.IZcglSuppliesService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 物资档案
 * @Author: jeecg-boot
 * @Date:   2022-10-27
 * @Version: V1.0
 */
@Api(tags="物资档案")
@RestController
@RequestMapping("/zcglsupplies/zcglSupplies")
@Slf4j
public class ZcglSuppliesController extends JeecgController<ZcglSupplies, IZcglSuppliesService> {
	@Autowired
	private IZcglSuppliesService zcglSuppliesService;

	 @Autowired
	 private IActBusinessService actBusinessService;
	 @ApiOperation(value = "固定资产-批量查询物资", notes = "固定资产-批量查询物资")
	 @GetMapping(value = "/getMulFixed")
	 public Result<?> getMulFixed(String id,
								  HttpServletRequest req) {
		 String[] ids = id.split(",");
		 //需要去获取真实的仓库数量
		 List<ZcglSupplies> assetFixeds = zcglSuppliesService.listByIds(Arrays.asList(ids));
		 for (int i = 0; i < assetFixeds.size(); i++) {
			 ZcglSupplies assetFixed = assetFixeds.get(i);
			 List<ActBusiness> list = actBusinessService.list(new QueryWrapper<ActBusiness>().eq("status", "1").eq("table_name", "zcgl_put_storage").like("data", assetFixed.getId()));
			 if (list.size() > 0) {
				 Double count = 0.0 ;
				 for (int j = 0; j < list.size(); j++) {
					 ActBusiness actBusiness = list.get(j);
					 JSONObject data = JSON.parseObject(actBusiness.getData());
					 JSONArray jsonArray = data.getJSONArray("zcglPutStorageAssetsList");
					 for (int k = 0; k < jsonArray.size(); k++) {
						 count += Double.valueOf(jsonArray.getJSONObject(k).get("assetAmount").toString());
					 }
				 }
//				 //显示流程中的数量
//				 assetFixed.setLcCount(new BigDecimal(count));
//				 //显示可以入库的数量 因为有的在流程中审核，已经被占用数量
//				 assetFixed.setTrueCount(assetFixed.getAmount().subtract(new BigDecimal(count)));
			 }else{

			 }
		 }
		 return Result.ok(assetFixeds);
	 }

	/**
	 * 分页列表查询
	 *
	 * @param zcglSupplies
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "物资档案-分页列表查询")
	@ApiOperation(value="物资档案-分页列表查询", notes="物资档案-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(ZcglSupplies zcglSupplies,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ZcglSupplies> queryWrapper = QueryGenerator.initQueryWrapper(zcglSupplies, req.getParameterMap());
		Page<ZcglSupplies> page = new Page<ZcglSupplies>(pageNo, pageSize);
		IPage<ZcglSupplies> pageList = zcglSuppliesService.page(page, queryWrapper);
		return Result.ok(pageList);
	}

	/**
	 *   添加
	 *
	 * @param zcglSupplies
	 * @return
	 */
	@AutoLog(value = "物资档案-添加")
	@ApiOperation(value="物资档案-添加", notes="物资档案-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody ZcglSupplies zcglSupplies) {
		//此时需要去判断该类型总共有多少条了，自动生成资产的编码
		int count = zcglSuppliesService.getTypeCount(zcglSupplies.getAssetType()) + 1;
		List<String> list = new ArrayList<String>();
		if (String.valueOf(count).length() < 4) {
			for (int i = 0; i < 4 - String.valueOf(count).length(); i++) {
				list.add("0");
			}
		}
		String result = list.toString().replace("[", "").replace("]", "") + String.valueOf(count);
		zcglSupplies.setDelFlag(0);
		zcglSupplies.setCode(zcglSupplies.getAssetType() + result.replace(", ", ""));
		zcglSuppliesService.save(zcglSupplies);
		return Result.ok("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param zcglSupplies
	 * @return
	 */
	@AutoLog(value = "物资档案-编辑")
	@ApiOperation(value="物资档案-编辑", notes="物资档案-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody ZcglSupplies zcglSupplies) {
		zcglSuppliesService.updateById(zcglSupplies);
		return Result.ok("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "物资档案-通过id删除")
	@ApiOperation(value="物资档案-通过id删除", notes="物资档案-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		zcglSuppliesService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "物资档案-批量删除")
	@ApiOperation(value="物资档案-批量删除", notes="物资档案-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.zcglSuppliesService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "物资档案-通过id查询")
	@ApiOperation(value="物资档案-通过id查询", notes="物资档案-通过id查询")
	@RequestMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		ZcglSupplies zcglSupplies = zcglSuppliesService.getById(id);
		if(zcglSupplies==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(zcglSupplies);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param zcglSupplies
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ZcglSupplies zcglSupplies) {
        return super.exportXls(request, zcglSupplies, ZcglSupplies.class, "物资档案");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ZcglSupplies.class);
    }

}
